package ui.strategy;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import tool.LongStringTransformer;
import tool.MyButton;
import tool.MyLabel;
import vo.DiscountVo;
import vo.UserVo;
import vo.Vo;

import javax.swing.JLabel;

import blservice.strategy.StrategyBLService;
import client.boot.BLServiceManager;

import java.awt.Font;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.awt.Color;

public class DiscountPanel extends JPanel{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	DiscountVo  vo;
	UserVo user;
	private JTextField total_require;
	private tool.DateChooserJButton tfFromDate;
	private tool.DateChooserJButton tfToDate;
	private JComboBox<String> client;
	private JTextField discount;

	/**
	 * 
	 * @param user
	 * @param vo
	 *            如果是单据查询，则传入vo，如果是单据制定则传入null;
	 * @param editable
	 *            如果是新建或者红冲并复制就设置成editable
	 * @param 红冲 
	 */
	public DiscountPanel(UserVo user) {
		setForeground(Color.WHITE);
		setBackground(Color.WHITE);
		this.user = user;
		this.setLayout(null);
		this.setOpaque(false);
	
		
		JLabel lblNewLabel = new JLabel("制定价格折让");
		lblNewLabel.setForeground(Color.WHITE);
		lblNewLabel.setFont(new Font("微软雅黑", Font.PLAIN, 18));
		lblNewLabel.setBounds(260, 0, 150, 20);
		add(lblNewLabel);
		
		

		JLabel lblFromDate = new MyLabel("起始时间：");
		lblFromDate.setBounds(137, 54, 65, 15);
		add(lblFromDate);

		tfFromDate = new tool.DateChooserJButton();
		tfFromDate.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
			}
		});
		tfFromDate.setBounds(259, 54, 105, 25);
		add(tfFromDate);

		JLabel lblToDate = new MyLabel("结束时间：");
		lblToDate.setBounds(137, 105, 70, 15);
		add(lblToDate);

		tfToDate = new tool.DateChooserJButton();
		tfToDate.setBounds(259, 103, 105, 25);
		add(tfToDate);
		
		JLabel label_2 = new MyLabel("客户级别：");
		label_2.setBounds(137, 169, 100, 15);
		add(label_2);
		
		client = new JComboBox<String>();
		client.setModel(new DefaultComboBoxModel<String>(new String[] { "一级",
				"二级", "三级", "四级", "五级" }));

		client.setBounds(259, 160, 105, 24);
		add(client);
		
		
		JLabel label_4 = new MyLabel("总额要求：");
		label_4.setBounds(138, 230, 80, 18);
		add(label_4);
		
		total_require = new JTextField();
		total_require.setColumns(10);
		total_require.setBounds(259, 229, 105, 21);
		add(total_require);
		

		

		JButton submit = new MyButton("Graphics/Button93-23.png");
		submit.setText("提交");
		submit.setBounds(271, 350, 93, 23);
		submit.addActionListener(e -> submit());		
		add(submit);
		
		JLabel label = new MyLabel("折让金额：");
		label.setBounds(137, 292, 70, 15);
		add(label);
		
		discount = new JTextField();
		discount.setBounds(259, 290, 105, 19);
		add(discount);
		discount.setColumns(10);
		
 }

	

	void clear(){
		total_require.setText("");
		discount.setText("");
       
	}

	

	void submit() {
		vo=new DiscountVo();
		if(total_require.getText().equals("")||discount.getText().equals("")){
			JOptionPane.showMessageDialog(this, "请完整价格折让策略！\n");
			return;
		}
		if(new SimpleDateFormat("yyyy-mm-dd").format(tfFromDate.getDate()).compareTo(new SimpleDateFormat("yyyy-mm-dd").format(tfToDate.getDate()))>0){
			JOptionPane.showMessageDialog(this, "起始日期大于结束日期！\n");
			return;
		}else{
			vo.setFrom(tfFromDate.getDate());
			vo.setTo(tfToDate.getDate());
			vo.set客户级别(client.getSelectedIndex());
			 try {
            	 vo.set总额条件(LongStringTransformer.StringToLong(total_require.getText()));
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(this, "总额要求格式错误\n" + e.getMessage());
				e.printStackTrace();
				return;
			}
			 try {
				 vo.setDiscount(LongStringTransformer.StringToLong(discount.getText()));
			} catch (NumberFormatException e) {
				JOptionPane.showMessageDialog(this, "折让金额格式错误\n" + e.getMessage());
				e.printStackTrace();
				return;
			}
			 if(vo.get总额条件()<vo.getDiscount()){
				 int rs=JOptionPane.showConfirmDialog(null, "您填写的金额要求小于折让金额！您确定？","警告:", JOptionPane.YES_NO_OPTION);
				  if(rs!=JOptionPane.OK_OPTION){
				       clear();
				       return;
				  }
			 }
			
		}
		StrategyBLService bl;
		try {
			bl = BLServiceManager.getStrategyBLService(null);
		        Vo result = bl.saveDiscount(vo);
				if (result.isSuccessed()) {
					JOptionPane.showMessageDialog(this, "价格折让制定成功\n");
					clear();

				} else {
					JOptionPane.showMessageDialog(this,
							"价格折让券制定失败\n" + result.getFailMessage());
				}
			
		} catch (RemoteException | MalformedURLException | SQLException
				| NotBoundException e) {
			JOptionPane.showMessageDialog(this, "无法连接到服务器\n" + e.getMessage());
			e.printStackTrace();
		}
		
	}

	
	
	
}
